package com.geniusk.app.controller;

import com.geniusk.common.core.JSONResult;
import com.geniusk.file.utils.MinioUtil;
import com.geniusk.system.model.bo.app.AppVersionBO;
import com.geniusk.system.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP更新下载接口
 */
@RestController
@RequestMapping("/app/update")
public class AppUpdateController {

    @Autowired
    private AppService appService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 获取最新APP版本
     */
    @PostMapping("/selectLatestAppVersion")
    public JSONResult selectLatestAppVersion(@Valid @RequestBody AppVersionBO appVersionBO) {
        String oldVersion = appVersionBO.getVersion();
        String version = appService.selectLatestAppVersion();
        if(version != null){
            Map<String, Object> result = new HashMap<>();
            result.put("version",version);
            if(!version.equals(oldVersion)){
                List<String> updateInfoList = appService.selectLatestAppUpdateInfo(oldVersion);
                result.put("updateInfoList",updateInfoList);
            }
            return JSONResult.ok(result);
        }else{
            return JSONResult.errorMsg("获取最新APP版本失败");
        }
    }

    /**
     * 获取最新应用下载地址
     */
    @GetMapping("/selectLatestApp")
    public JSONResult selectLatestAppDownloadUrl() {
        String url = appService.selectLatestAppUrl();
        String downloadUrl = minioUtil.getFileUrl(url);
        Map<String, Object> result = new HashMap<>();
        result.put("downloadUrl",downloadUrl);
        return JSONResult.ok(result);
    }
}
